import { fetchTenantList,doAddTenant,doEditTenant,fetchMenuTree,doRemoveList,doResetPassword,fetchMenuTreeByAdmin} from '@/services/tenantManagement';
export default {
  namespace:'tenantManagement',
  state: {
    total:0,
    items:[],
    loading:true
  },
  effects: {
    *getTenantList({ payload, callback }, { call, put }) {
      const response = yield call(fetchTenantList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getMenuTree({ payload, callback }, { call, put }) {
      const response = yield call(fetchMenuTree, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getMenuTreeByAdmin({ payload, callback }, { call, put }) {
      const response = yield call(fetchMenuTreeByAdmin, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *addTenant({ payload, callback }, { call, put }) {
      const response = yield call(doAddTenant, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *editTenant({ payload, callback }, { call, put }) {
      const response = yield call(doEditTenant, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *removeList({ payload, callback }, { call, put }) {
      const response = yield call(doRemoveList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *toResetPassword({ payload, callback }, { call, put }) {
      const response = yield call(doResetPassword, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
